/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graphviewer;

import service.Service;
import service.ServiceMouse;
import java.awt.*;
import java.util.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import service.Storage;

/**
 *
 * @author Sabitov Artem
 * @since 08.07.2013
 */
public class GraphViewer extends JFrame implements KeyListener {
	public static final int FRAME_WIDTH = 1024;
	public static final int FRAME_HEIGHT = 768;
	public static final int FRAME_LOCATION_X = 32;
	public static final int FRAME_LOCATION_Y = 8;
	public static final int OFFSET_TO_MOVE = 50;
	
	private HashMap<String, ArrayList<Node>> graphs;
	private Graph[] graph;	
	private boolean calculated;
	private int threadCount;
	
	
	public GraphViewer() {
		this("Title not found.");
	}
	
	public GraphViewer(String title) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(FRAME_WIDTH, FRAME_HEIGHT);
		setLocation(FRAME_LOCATION_X, FRAME_LOCATION_Y);
		setTitle(title);
		getContentPane().setBackground(Color.GRAY);
		setVisible(true);
		runServiceFunctions();
	}
	
	private void runServiceFunctions() {
		Storage.setViewer(this);
		this.addKeyListener(this);
		this.add(new ServiceMouse(this));
		Thread threadSpawn = new Thread(new Runnable() {
			@Override
			public void run() {
				Parser prs = new Parser();
				graphs = prs.getGraphs();
				graph = new Graph[graphs.size()];
				for(int i = 0; i < graphs.size(); i++) {
					graph[i] = new Graph(graphs.get("ID_" + i));
				}
				if(Service.getServiceState()) {
					Storage.getService().updateStateService();
				}
			}
		});
		threadSpawn.start();
	}

	private void runCalculation() {
		if(!calculated) {
			Thread threadCalculation = new Thread(new Runnable() {
				@Override
				public void run() {
					calculated = true;
					for(int i = 0; i < graph.length; i++) {
						GraphCalculate gc = new GraphCalculate(graph[i]);
					}
					log("calculation is completed!");
				}
			});
			threadCalculation.start();
			calculated = false;
		} else {
			log("calculation is already running!");
		}
		runAlignment();
	}
	
	private void runAlignment() {
		Thread threadScaling = new Thread(new Runnable() {
			@Override
			public void run() {
				GraphScale gs = new GraphScale(graph);
			}
		});
		threadScaling.start();
	}
	
	private void log(Object obj) {
		if(Service.getServiceState()) {
			Service.log(obj);
		}
	}

	@Override
	public void keyTyped(KeyEvent ke) {
	}
	@Override
	public void keyPressed(KeyEvent ke) {
		int key = ke.getKeyCode();
		if(key == 32) {
			runCalculation();
		} else if(key == 37) { // left
			moveGraph((-1)*OFFSET_TO_MOVE, 0);
		} else if(key == 38) { // up
			moveGraph(0, (-1)*OFFSET_TO_MOVE);
		} else if(key == 39) { // righ
			moveGraph(OFFSET_TO_MOVE, 0);
		} else if(key == 40) { // down
			moveGraph(0, OFFSET_TO_MOVE);
		}
	}
	
	private void moveGraph(int offsetX, int offsetY) {
		for(int i = 0; i < graph.length; i++) {
			for (int j = 0; j < graph[i].getSizeGraph(); j++) {
				Node node = graph[i].getNode(j);
				node.setCoords(node.getCoordX() + offsetX, node.getCoordY() + offsetY);
			}
			graph[i].update();
		}
	}
	
	@Override
	public void keyReleased(KeyEvent ke) {
	}
}